<?php 

/**
 * 积分规则
 */

namespace app\admin\controller;

use \think\Request;

class IntegralRule extends Base{

    public function index() {
        $model = model('Integralrule');

        if($this->request->isAjax()){
            $data = $this->request->param();
            $data['tenantId'] = session('TENANT_ID');
            $where = ['tenantId'=>$data['tenantId']];
            $find = $model->where($where)->find();
            $data['rechargeNum'] = $data['rechargeNum'] * 100;
            if($find){
                $data['updateDate'] = date('Y-m-d H:i:s',time());
                $data['updateOper'] = session('member')['id'];
                $result = $model->where($where)->update($data);
            } else {
                $data['createDate'] = date('Y-m-d H:i:s',time());
                $data['createOper'] = session('member')['id'];
                $result = $model->insert($data);
            }

            if($result){
                return json(['status' => '1', 'msg' => '保存成功！']);
            }else{
                return json(['status' => '0', 'msg' => '保存失败！']);
            }
        }

        $where = [
            'tenantId' => session('TENANT_ID')
        ];
        $info = $model->where($where)->find();

        $this->assign('info', $info);

        return $this->fetch("integral:rule");
    }
}